package cn.com.taiji.utils;

import java.sql.SQLException;
import java.util.UUID;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;

public class InsertResource {
	public static void insertController(String tableName) {
		String aString = UUID.randomUUID().toString().replaceAll("-", "");
		String RESOURCE_ID = aString.substring(5, aString.length());
		String bString = UUID.randomUUID().toString().replaceAll("-", "");
		String RESOURCE_ROLE_ID = bString.substring(5, bString.length());
		System.out.println(RESOURCE_ID);
		System.out.println(RESOURCE_ROLE_ID);
		String url = "app/acl/" + StrKit.toCamelCase(tableName) + "/manage";
		Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				// TODO Auto-generated method stub
				try {
					int i = Db.update(
							"insert into SAMPLE_RESOURCE t (ID,LIST,LOGO_PIC,MENU_TYPE,NAME,TYPE_CODE,URL,REQUEST_METHOD) values"
									+ "(?,10,'default.gif','COLUMN',?,'B_SAMPLE',?,'GET')",
							RESOURCE_ID, tableName, url);
					int k = Db.update(
							"insert into SAMPLE_ROLE_RESOURCE t (ID,RESOURCE_ID,ROLE_ID) values (?,?,'admin') ",
							RESOURCE_ROLE_ID, RESOURCE_ID);

					if (i > 0 && k > 0) {
						System.out.println("新增controller已经添加到admin用户下");

					}
					return true;
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					return false;
				}
			}
		});

	}
}
